package com.yylending.las.personal.bandcard;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.secure.Cryptos;
import com.yylending.las.Global;
import com.yylending.las.YJKD;
import com.yylending.las.common.AESEntity;
import com.yylending.las.common.utils.preferenceutils.PreferenceConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by HenryZhang on 2016/11/10.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class BankCardPresenter extends Presenter<BankCardModel, BankCardPresenter.View> {
	private final static String TAG = "BankCardPresenter";
	private static final String MSG_GET_SUPPORT_BANK_FAILED = "获取银行卡信息失败";
	private static final String MSG_BIND_CARD_FAILED = "系统繁忙，请稍后再试";
	private static final String RET_CODE_BIND_CARD_ONLY_ONE = "100017"; // 只能绑定一张银行卡
	private static final String RET_CODE_BIND_CARD_REQUEST_TOO_MUCH = "100021"; // 请勿频繁请求发送
	private static final String RET_CODE_BIND_CARD_BANK_NAME_EMPTY = "100030"; // 开户行不能为空
	private static final String RET_CODE_BIND_CARD_CARD_NUM_EMPTY = "100031"; // 银行卡号不能为空
	private static final String RET_CODE_BIND_CARD_NUM_ERROR = "100032"; // 银行卡号格式不正确
	private static final String RET_CODE_BIND_CARD_ACCOUNT_NAME_EMPTY = "100033"; // 开户人不能为空
	private static final String RET_CODE_BIND_CARD_CARD_INFO_ERROR = "100035"; // 卡信息有误
	private static final String RET_CODE_BIND_CARD_NOT_SUPPORT_THIS_BANK = "100036"; // 暂不支持该银行卡
	private static final String RET_CODE_BIND_CARD_HAS_BEEN_BIND = "100037"; // 银行卡已被绑定

	public static class Generator implements PresenterFactory<BankCardPresenter> {
		@Override
		public BankCardPresenter create() {
			return new BankCardPresenter();
		}
	}

	public interface View extends IView {
		void bindCardSuccess();
		void bindCardFailed(String msg);

		void hasBindBankCard(boolean isManual, List<BoundBankCards.BankInfo> bankInfo);
		void neverBindBankCard(boolean isManual);
		void requestBindBankCardFailed(boolean isManual);

		void getSupportBankSuccess(List<BankCardModel.SupportBankList.SupportBank> supportBankList);
		void getSupportBankFailed(String msg);

		void bindCardRetCode(String retCode);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new BankCardModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

    /**
     * 获取支持的银行卡列表及代码
     */
    void getSupportBankList() {
        execute(null, BankCardModel.METHOD_SUPPORT_BANK, false, null, mRequestPage);
    }

    /**
     * 绑定银行卡
     */
    void bindBankCard(String name, String bank, String bankNumber, String mobile) {
        execute(new AESEntity(new BankCardModel.BindCard(name, bank, bankNumber, mobile)), BankCardModel.METHOD_BIND_BANK_CARD, true, null, mRequestPage);
    }

    /**
     * 获取已绑定银行卡列表
     * @param isManual 是否是手动操作获取.
     */
    public void getMyBankCard(boolean isManual) {
        if (isManual) {
            execute(null, BankCardModel.METHOD_GET_BANKCARD_MANUAL, false, null, mRequestPage);
        } else {
            execute(null, BankCardModel.METHOD_GET_BANKCARD_BACKGROUND, false, null, mRequestPage);
        }
    }

	/**
	 * 支持的银行卡列表.
	 */
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getSupportBankListResult(BankCardModel.BankListResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				BankCardModel.SupportBankList supportBankList = result.getData();
				if (supportBankList != null) {
					if (supportBankList.bankList.size() > 0) {
						mView.getSupportBankSuccess(supportBankList.bankList);
						return;
					}
				}
				mView.getSupportBankFailed(MSG_GET_SUPPORT_BANK_FAILED);
			} else {
				mView.getSupportBankFailed(MSG_GET_SUPPORT_BANK_FAILED);
			}
		}
	}

	/**
	 * 获取已绑定银行卡列表
	 */
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void myBankCard(BankCardModel.MyBankCardResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				BankCardModel.AesJson aesJson = result.getData();
				if (aesJson != null && !TextUtils.isEmpty(aesJson.json)) {
					BoundBankCards boundBankCards = new Gson().fromJson(Cryptos.aesDecrypt(aesJson.json, Global.KEY_AES), BoundBankCards.class);
					if (null != boundBankCards) {
						if (boundBankCards.bankCardList.size() > 0) {
							mView.hasBindBankCard(result.requestCode == BankCardModel.METHOD_GET_BANKCARD_MANUAL, boundBankCards.bankCardList);
							return;
						}
					}
				}
				mView.neverBindBankCard(result.requestCode == BankCardModel.METHOD_GET_BANKCARD_MANUAL);
			} else {
				mView.requestBindBankCardFailed(result.requestCode == BankCardModel.METHOD_GET_BANKCARD_MANUAL);
			}
		}
	}

	/**
	 * 绑定银行卡结果
	 */
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void bankCardResult(BankCardModel.BindCardResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			String errMsg = "";
			if (result.getData() != null && !TextUtils.isEmpty(result.getData().msg)) {
				errMsg = result.getData().msg;
			}

			if (BaseResultEntity.isResponseSuccess(result)) {
				if (result.getData() != null && !TextUtils.isEmpty(result.getData().userName)) {
					Global.USER_NAME = result.getData().userName;
					YJKD.getInstance().getPreferenceManager().savePreference(PreferenceConstants.SP_KEY_USER_NAME, result.getData().userName);
				}
				mView.bindCardSuccess();
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_CARD_INFO_ERROR, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "卡信息有误" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_REQUEST_TOO_MUCH, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "请勿频繁请求发送" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_ONLY_ONE, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "只能绑定一张银行卡" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_NUM_ERROR, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "银行卡号格式不正确" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_NOT_SUPPORT_THIS_BANK, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "暂不支持该银行卡" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_BANK_NAME_EMPTY, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "开户行不能为空" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_CARD_NUM_EMPTY, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "银行卡号不能为空" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_ACCOUNT_NAME_EMPTY, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "开户人不能为空" : errMsg);
			} else if (TextUtils.equals(RET_CODE_BIND_CARD_HAS_BEEN_BIND, result.getRetCode())) {
				mView.bindCardFailed(TextUtils.isEmpty(errMsg) ? "银行卡已被绑定" : errMsg);
			} else {
				if (TextUtils.isEmpty(errMsg)) {
					if (!TextUtils.isEmpty(result.getRetMsg())) {
						errMsg = result.getRetMsg();
					}
				}
				if (TextUtils.isEmpty(errMsg)) {
					errMsg = MSG_BIND_CARD_FAILED;
				}
				mView.bindCardFailed(errMsg);
			}
			mView.bindCardRetCode(result.getRetCode());
		}
	}
}
